<?
function drizzo_get_the_trash($id = 0) {
	drizzo_db_connect;

	global $drizzo_trashheap;
	
	$the_post = mysql_query("SELECT * FROM `$drizzo_trashheap` WHERE `ID`='$id'");
	if(!$the_post) {
		echo "<p>This post could not be found.</p>";
		return false;
	} else {
		while($row = mysql_fetch_array($the_post)) {
			$the_post = array(
				"ID" => $id,
				"author" => $row['author'],
				"type" => $row['type'],
				"status" => $row['status'],
				"page_parent" => $row['page_parent'],
				"page_order" => $row['page_order'],
				"title" => stripslashes($row['title']),
				"content" => stripslashes($row['content']),
				"tags" => $row['tags'],
				"comment_status" => $row['comment_status'],
				"posted_date" => date("M d, Y - g:i A", $row['posted_date']),
				"posted_date_raw" => $row['posted_date'],
				"edited_date" => date("M d, Y - g:i A", $row['edited_date']),
				"edited_date_raw" => $row['edited_date'],
				"edited_by" => $row['edited_by'],
				"menu_name" => $row['menu_name'],
				"permalink_name" => $row['permalink_name'],
				"lat" => $row['lat'],
				"lon" => $row['lon'],
				"old_id" => $row['old_id']
			);

			return $the_post;

		}
	}
}

function drizzo_get_the_trashheap($limit = 20, $offset = 0) {
	global $drizzo_trashheap, $drizzo_comments, $drizzo_users;
	
	$types = $type_string;
	
	drizzo_db_connect();

	$the_posts_query = mysql_query("SELECT * FROM `$drizzo_trashheap` ORDER BY `ID` DESC LIMIT $offset,$limit");

	if(!$the_posts_query) {
		echo "<p>Posts could not be retrieved.</p>";
	} else {
		while($row = mysql_fetch_array($the_posts_query)) {
			$author_query = mysql_query("SELECT `nice_name`,`username` FROM `$drizzo_users` WHERE `ID`='$row[author]'");
			if($author_query) {
				while($author_row = mysql_fetch_array($author_query)) {
					$author = $author_row['nice_name'];
					if($author == "") {
						$author = $author_row['username'];
					}
				}				
			}

			$comments_query = mysql_query("SELECT * FROM `$drizzo_comments` WHERE `post_id`='$row[old_id]'");
			if($comments_query) {
				$comments = mysql_num_rows($comments_query);				
			} else {
				$comments = 0;
			}

			$the_posts[] = array(
				"ID" => $row['ID'],
				"author" => $row['author'],
				"author_nicename" => $author,
				"type" => $row['type'],
				"status" => $row['status'],
				"page_parent" => $row['page_parent'],
				"page_order" => $row['page_order'],
				"title" => stripslashes($row['title']),
				"content" => nl2br(html_entity_decode(stripslashes($row['content']))),
				"tags" => $row['tags'],
				"comment_status" => $row['comment_status'],
				"comment_count" => $comments,
				"posted_date" => date("M d, Y - g:i A", $row['posted_date']),
				"posted_date_raw" => $row['posted_date'],
				"edited_date" => date("M d, Y - g:i A", $row['edited_date']),
				"edited_date_raw" => $row['edited_date'],
				"edited_by" => $row['edited_by'],
				"menu_name" => $row['menu_name'],
				"permalink_name" => $row['permalink_name'],
				"lat" => $row['lat'],
				"lon" => $row['lon']
				);
		}
	}
	return $the_posts;
}

function drizzo_move_to_trashheap() {
	global $drizzo_trashheap, $_POST, $session_user_id;

	$old_id = $_POST['old_id'];
	$old_author = $_POST['old_author'];
	$old_type = $_POST['old_type'];
	$old_status = $_POST['old_status'];
	$old_page_parent = $_POST['old_page_parent'];
	$old_page_order = $_POST['old_page_order'];
	$old_title = addslashes(html_entity_decode($_POST['old_title']));
	$old_content = addslashes(html_entity_decode($_POST['old_content']));
	$old_tags = addslashes(html_entity_decode($_POST['old_tags']));
	$old_comment_status = $_POST['old_comment_status'];
	$old_edited_by = $_POST['old_edited_by'];
	$old_edited_date = $_POST['old_edited_date'];
	$old_date = $_POST['old_date'];
	$old_permalink_name = $_POST['old_permalink_name'];
	$old_menu_name = $_POST['old_menu_name'];
	$old_lat = $_POST['old_lat'];
	$old_lon = $_POST['old_lon'];
	$deleter = $session_user_id;

		$trashheap = mysql_query("INSERT INTO `$drizzo_trashheap` SET `author`='$old_author',`type`='$old_type',`status`='$old_status',`page_parent`='$old_page_parent',`page_order`='$old_page_order',`title`='$old_title',`content`='$old_content',`tags`='$old_tags',`comment_status`='$old_comment_status',`edited_by`='$old_edited_by',`edited_date`='$old_edited_date',`posted_date`='$old_date',`permalink_name`='$old_permalink_name',`menu_name`='$old_menu_name',`lat`='$old_lat',`lon`='$old_lon',`old_id`='$old_id',`deleted_date`=" . time() . ",`deleted_by`='$deleter'");

	if($trashheap) {
		return true;
	} else {
		return false;
	}
}
?>